﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using SafoverWpfFw;
using System.Diagnostics;
using Safover.LidarCollector.ViewModel;

namespace Safover.LidarCollector.View
{
    static class ApplicationPageHelpers
    {
        /// <summary>
        /// Takes a <see cref="ApplicationPage"/> and a view model, if any, and creates the desired page
        /// </summary>
        /// <param name="page"></param>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public static BasePage ToBasePage(this ApplicationPage page, object viewModel = null)
        {
            return page.To(viewModel);
        }

        /// <summary>
        /// Converts a <see cref="BasePage"/> to the specific <see cref="ApplicationPage"/> that is for that type of page
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static ApplicationPage ToApplicationPage(this BasePage page)
        {
            return page.To();
        }

        private static BasePage To(this ApplicationPage page, object viewModel = null)
        {
            // Find the appropriate page
            switch (page)
            {
                case ApplicationPage.Login:
                    return new LoginPage(viewModel as LoginPageViewModel);

                case ApplicationPage.Collection:
                    return new CollectionPage(viewModel as CollectionPageViewModel);

                case ApplicationPage.Config:
                    return new ConfigPage(viewModel as ConfigPageViewModel);

                case ApplicationPage.Camera:
                    return new CameraPage(viewModel as CameraPageViewModel);
                default:
                    Debugger.Break();
                    return null;
            }
        }

        private static ApplicationPage To(this BasePage page) 
        {
            // Find application page that matches the base page
            if (page is LoginPage)
                return ApplicationPage.Login;

            if (page is CollectionPage)
                return ApplicationPage.Collection;

            if (page is ConfigPage)
                return ApplicationPage.Config;

            if (page is CameraPage)
                return ApplicationPage.Camera;

            // Alert developer of issue
            Debugger.Break();
            return default(ApplicationPage);
        }
    }
}
